#include <stdio.h>
#include <stdlib.h>

int main (int argc, char const* argv[])
{

    double a[6][6];
    int b[13];
    int n;
    int flag = 0;
    int cases;

    scanf("%d", &cases);

    while (cases--) {
        int i, j;

        if (flag) {
            printf("\n");
        }

        flag = 1;
        for (i = 1; i <=5; i++) {
            for (j = 1; j <=5; j++) {
                scanf("%lf", &a[i][j]);
            }
        }

        while (scanf("%d", &n) && n) {
            b[1] = 1;
            for (i = 2; i <= n+1; i++) {
                scanf("%d", &b[i]);
            }
            b[i] = 1;

            double m;
            scanf("%lf", &m);
            for (i = 2; i <= n+2; i++) {
                m = m * a[b[i-1]][b[i]];
                m = (int)(m*100+0.5);
                m /= 100;
            }
            printf("%.2lf\n", m);
        }
    }

    return 0;
}
